package cn.itsource.pet.controller;

import cn.itsource.basic.util.CurrentUserUtil;
import cn.itsource.basic.util.PageBean;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.pet.domain.dto.OrderPetAcquisitionConfirmDto;
import cn.itsource.pet.service.IOrderPetAcquisitionService;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.query.OrderPetAcquisitionQuery;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {
    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;
    @Autowired
    private IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param orderPetAcquisition  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        if( orderPetAcquisition.getId()!=null)
            orderPetAcquisitionService.update(orderPetAcquisition);
        else
            orderPetAcquisitionService.add(orderPetAcquisition);
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        orderPetAcquisitionService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderPetAcquisition get(@PathVariable("id")Long id)
    {
        return orderPetAcquisitionService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderPetAcquisition> list(){
        return orderPetAcquisitionService.queryAll();
    }


    /**
    * 分页查询数据
    * 添加查询条件
     * 如果当前登录用户有shopid那么查询条件就添加shopid
     * 如果没有就不添加
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageBean<OrderPetAcquisition> json(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) throws Exception {
        Long empId = CurrentUserUtil.getCurrentUtil(request);
        Employee employee = employeeService.queryById(empId);
        if(employee != null){
            if(employee.getShop_id() != null){
                query.setShopId(employee.getShop_id());
            }
        }
        return orderPetAcquisitionService.pageList(query);
    }

    /**
     * 确认收狗单
     * @param dto
     * @return
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderPetAcquisitionConfirmDto dto){
        return orderPetAcquisitionService.confirm(dto);
    }
}
